﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.DirectoryServices.ActiveDirectory;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Forms.VisualStyles;
using System.Windows.Shapes;
using Macad.Common;
using Macad.Core;
using Macad.Core.Shapes;
using Macad.Core.Topology;
using Macad.Interaction;
using Macad.Occt;
using SpCad.Tools;
using static Macad.Interaction.ToggleSubshapesAction;

namespace SpCad.Core.Modify
{
    /// <summary>
    /// 焊缝选择
    /// </summary>
    public class Weld : EdgeModifierBase
    {
        List<ShapeBase> ListEdge { get; set; } = new List<ShapeBase>();
        public static Weld Create(Body targetBody)
        {
            var edgeModelSelect = new Weld();
            targetBody.AddShape(edgeModelSelect);
            return edgeModelSelect;
        }
        #region Make

        //GetSubshapeReference
       
        protected override bool MakeInternal(MakeFlags flags = MakeFlags.None)
        {
            ClearSubshapeLists();

           // AIS_Shape 
            if (Operands.Count < 1)
            {
                Messages.Error("需要选择一个形状");
                return false;
            }
           
            var sourceShape = GetOperandBRep(0);
            if (sourceShape == null)
                return false;

            // If no edges are selected, just copy the source shape
            if (Edges.Length == 0)
            {
                ListEdge?.Clear();
                return Skip();
            }
           
            var edges = GetOcEdges().ToArray();
           
            if (!edges.Any())
            {
                Messages.Error("找不到边框.您需要重新选择要修剪的边缘");
                return false;
            }
            ListEdge?.Clear();
            ClearColor();
            //edges.ForEach(c => ListEdge?.Add(c.TopoDS_EdgeToShapeTools())) ;
            foreach (var edge in edges)
            {
                ListEdge?.Add(edge.TopoDS_EdgeToShapeTools());
                //Subshape
                SetColor(edge);
             //  var k= (AIS_Shape)edge;
         
            }
            return base.MakeInternal(flags);
        }
       
        AIS_Shape _AisShape;
        private void SetColor( TopoDS_Shape edg)
        {
           
            _AisShape = new AIS_Shape(edg); 
            _AisShape.SetZLayer(-2); // Graphic3d_ZLayerId_Top
            _AisShape.SetColor(new Color(1f, 0.0f, 1.0f).ToQuantityColor());
            _AisShape.Attributes().LineAspect().SetColor(new Color(0.0f, 0.0f, 1.0f).ToQuantityColor());
            _AisShape.SetWidth(3);
            _AisShape.Attributes().PointAspect().SetScale(3);

            InteractiveContext.Current.WorkspaceController.Workspace.AisContext.Display(_AisShape, false);
            InteractiveContext.Current.WorkspaceController.Workspace.AisContext.Deactivate(_AisShape);
        }

        private void ClearColor()
        {
            if (_AisShape != null)
            {
                // Remove AIS Shape
                InteractiveContext.Current.WorkspaceController.Workspace.AisContext.Remove(_AisShape, false);
                InteractiveContext.Current.WorkspaceController.Workspace.AisContext.Erase(_AisShape, false);
                _AisShape = null;
            }
        }
        #endregion
    }
}
